package sdu.attendancechecker.servlet.teacher;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import sdu.attendancechecker.dto.Attandance;
import sdu.attendancechecker.dto.Group;
import sdu.attendancechecker.dto.Schedule;
import sdu.attendancechecker.dto.Teacher;
import sdu.attendancechecker.models.ScheduleModel;
import sdu.attendancechecker.models.SubjectModel;
import sdu.attendancechecker.services.TeacherService;

public class AttListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession();
		Teacher t = (Teacher) session.getAttribute("teacher");

		if (request.getParameter("group") != null
				&& request.getParameter("subject") != null) {
			String subject = request.getParameter("subject");
			String group = request.getParameter("group");
			ArrayList<Group> groups = (ArrayList<Group>) session
					.getAttribute("groups");
			ArrayList<Attandance> attendance = null;
			for (int i = 0; i < groups.size(); i++) {
				if (groups.get(i).getName().equals(group)) {
					attendance = SubjectModel.getAttendance(groups.get(i)
							.getStudents(), subject);
					break;
				}
			}
			ArrayList<String> dates = new ArrayList<String>();
			Date date = new Date();
			// change format
			SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
			ArrayList<Schedule> schdl = ScheduleModel
					.getSchedule(subject, t.getId());
			int maxDate = 5;
			while (dates.size() < maxDate) {
				for (Schedule x : schdl) {
					if (x.getWeek() == (date.getDay() == 0 ? 7 : date.getDay())) {
						dates.add(0, sdf.format(date));
					}
					if (dates.size() >= maxDate)
						break;
				}
				date.setDate(date.getDate() - 1);
			}
			ArrayList<Attandance> att2 = new ArrayList<Attandance>();
			for (int i = 0; i < attendance.size(); i++) {
				Attandance ta = attendance.get(i);
				att2.add(new Attandance(ta.getStudent(), dates,
						new ArrayList<String>(), new ArrayList<Integer>(),
						new ArrayList<Integer>()));
			}
			for (String x : dates) {
				for (int i = 0; i < attendance.size(); i++) {
					Attandance ta = attendance.get(i);
					boolean flag = false;
					for (int j = 0; j < ta.getDates().size(); j++) {
						if (x.equals(ta.getDates().get(j))) {
							int att = ta.getAtt().get(j);
							att2.get(i).getAtt().add(att);
							flag = true;
							break;
						}
					}
					if (!flag) {
						att2.get(i).getAtt().add(0);
					}
				}
			}
			//String path = "C:\\Java\\Excel\\";
			//TeacherService.export(dates, att2, path, response);
			session.setAttribute("dates", dates);
			session.setAttribute("attendance", att2);
			response.sendRedirect("../teacher/group");
		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}
}
